package com.wang.mall.exception;

import com.wang.mall.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    public R handlerException(Exception u) {
        if (u instanceof UsernameNotFoundException) {
            return R.fail(401, "登陆失败");
        } else if (u instanceof IllegalArgumentException) {
            String message = ((IllegalArgumentException) u).getMessage();
            return R.fail(500, message);
        }
        log.error("服务器内部异常={}", u);
        return R.fail(500, "未知异常信息");
    }
}
